﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Setup_TableXml : System.Web.UI.Page
{
    string tab = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        tab=Request.QueryString["tab"];
        string TableXmlpath = Server.MapPath("~/configs/tables/" + tab + "/");

        if (!Directory.Exists(TableXmlpath))
        {
            Directory.CreateDirectory(TableXmlpath);
        }

        DirectoryInfo dir = new DirectoryInfo(TableXmlpath);
        var files = from f in dir.GetFiles()
                    select new { FileName = f.Name.Replace(".xml",""),TableName=tab };

        this.GridView1.DataSource = files;
        this.GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        tab=Request.QueryString["tab"];
        string xmlname = this.TextBox1.Text;
        string path = Server.MapPath("~/configs/tables/" + tab + "/" + xmlname + ".xml");
        if (File.Exists(path))
        {
            Helper.Alert("文件已存在");
        }
        else
        {            
            new ConfigTable(tab,xmlname).ObjToXml(path);
            Response.Redirect(this.Request.Url.ToString());
        }        
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string pk = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
        string file = Server.MapPath("~/configs/tables/" + tab + "/" + pk + ".xml");
        if (File.Exists(file))
        {
            File.Delete(file);
        }
        Response.Redirect(this.Request.Url.ToString());
    }
}
